function pxRatio(){
  var ratio = 0;
  if(window && window.document){
    var div = document.createElement('div'); 
    div.style.width = '1cm'; 
    div.id = 'puc'; 
    document.body.appendChild(div); 
    var w = getComputedStyle(div, null).width; 
    ratio = w.substr(0, w.length - 2); 
    div.parentNode.removeChild(div); 
  }
  return ratio
}

function pickNumber( str = '' ){
  str = String(str)
  let re = str.replace(/[^0-9|^\.|^\-]{1,}/g, '')
  return re ? Number(re) : 0
}
class PxRatio{
  _ratio = 0
  constructor(){
    this._ratio = pxRatio()
  }

  get ratio(){
    if( !this._ratio ){
      this._ratio = pxRatio()
    }
    return this._ratio
  }

  mm2px( mm ){
    const ratio = this._ratio
    var r = ratio / 10;
    return  mm  * r; 
  }

  px2mm( px ){
    const ratio = this._ratio
    var r = ratio / 10;
    return  px  / r; 
  }

  getNumber( str = '' , ex = 'px'){
    str = String(str)
    str = str.replace(/\s{1,}/g, '')
    let dw = str.substring(str.length-2).toLowerCase()    
    const nub = pickNumber(str)
    dw = dw.replace(/[0-9]{1,}/g, '')
    dw = dw ? dw : 'px'
    if( dw === ex ){
      return nub
    }else if( this[ dw + '2' + ex ] ){
      return this[ dw + '2' + ex ](nub)
    }
    return null
  }
}

export default new PxRatio()